<?php

class StudentPlace_StudentSemesterController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function setAction()
    {
        $this->getHelper('layout')->disableLayout();
        $yearSemester = $this->_getParam('yearSemester');
        $number = $this->_getPAram('number') ;
        $studentSn = (int)$this->_getParam('student_sn');

        foreach ($yearSemester as $ys=>$cs) {
            $tempArr = explode("-", $ys);
            $year= (int) $tempArr[0];
            $semester = (int) $tempArr[1];
            $tempArr = explode("-", $cs);
            $grade = (int) $tempArr[0];
            $classSn = (int) $tempArr[1];
            $res = Doctrine_Query::create()
            ->select('sn')
            ->from('Student_Model_StudentSemester')
            ->where('year=?', $year)
            ->andWhere('semester=?', $semester)
            ->andWhere('grade=?', $grade)
            ->andWhere('class_sn=?', $classSn)
            ->andWHere('student_sn=?',$studentSn)
            ->fetchOne();


            $studentSemester = new Student_Model_StudentSemester();
            if ($res)
            $studentSemester->assignIdentifier($res->sn);

            $studentSemester->year = $year;
            $studentSemester->semester = $semester;
            $studentSemester->grade = $grade;
            $studentSemester->student_sn = $studentSn ;
            $studentSemester->class_sn = $classSn;
            $studentSemester->number = (int)$number[$ys];
            $studentSemester->save();
        }
        $this->_helper->json(true) ;
    }

    public function getStudentSemesterAction()
    {
        $this->getHelper('layout')->disableLayout();
        $f = new Zend_Filter_StripTags();
        $studentId = $f->filter($this->_getParam('id'));
        // 先查學生入學年
        $student = Doctrine_Query::create()
        ->select('sn, name, study_condition, enroll_year')
        ->from('Student_Model_Student')
        ->where('student_id=?', $studentId)
        ->fetchOne();
        if ($student) {
            //            //共有幾學年
            //            $totalYear = $this->view->highYear - $this->view->lowYear;
            //            echo $totalYear.'<br>';
            // 找出學生的目前學期記錄
            $q = Doctrine_Query::create()
            ->select('student_sn, year, semester, grade, class_sn, number')
            ->from('Student_Model_StudentSemester')
            ->Where('student_sn=?',$student->sn) ;
            $arr = array();
            foreach($q->fetchArray() as $row)
            $arr[$row['year'].'-'.$row['semester']] = $row;

            $this->view->student = $student;
            $this->view->studentSemester = $arr;

        }
    }

}


